package com.eco.netty;

import com.alibaba.fastjson.JSONObject;
import com.eco.utils.ApplicationContextProvider;
import com.eco.utils.NettyCtxMap;
import com.github.benmanes.caffeine.cache.Cache;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/Sensor2HexHandler.class */
public class Sensor2HexHandler extends ChannelInboundHandlerAdapter {
    private NettyClient nettyClient;
    private Cache<String, Object> jklsCache;
    private Long startTime;
    private static final Logger log = LoggerFactory.getLogger((Class<?>) Sensor2HexHandler.class);
    private static JSONObject result = new JSONObject();

    public Sensor2HexHandler() {
        new ApplicationContextProvider();
        this.nettyClient = (NettyClient) ApplicationContextProvider.getBean(NettyClient.class);
        new ApplicationContextProvider();
        this.jklsCache = (Cache) ApplicationContextProvider.getBean(Cache.class);
        this.startTime = null;
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE) {
                ctx.channel().close();
                return;
            }
            return;
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("进口流速tcp有新连接");
        NettyCtxMap.add("sensor2", ctx);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String host = this.nettyClient.getSensor2Host();
        int port = this.nettyClient.getSensor2Port();
        log.error("与进口流速tcp服务" + host + ":" + port + "连接断开!");
        ctx.close();
        ctx.deregister();
        ctx.pipeline().remove(this);
        this.nettyClient.sensor2Reconnect(host, Integer.valueOf(port));
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            this.jklsCache.put("jkls", msg);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("与进口流速tcp服务连接异常!");
        ctx.close();
    }
}